import {
  devBaseUrl,
  prodBaseUrl,
  allPermission
} from "@/config";
import {
  getToken,
} from '@/utils/auth.js'
import store from '@/store/index.js'
import BigNumber from 'bignumber.js'
import {
  isArray
} from 'roc-utils'

/**
 * 获取基础url
 */
export function getBaseUrl() {
  return process.env.NODE_ENV == "development" ? devBaseUrl : prodBaseUrl
}

/**
 * 字符权限校验
 * @param {Array} value 校验值
 * @returns {Boolean}
 */
export function hasPermi(value) {

  if (value && isArray(value) && value.length > 0) {
    const permissions = store.state.user.permissions
    const permissionDatas = value
    const all_permission = allPermission;
    const hasPermission = permissions.some(permission => {
      return permission === all_permission || permissionDatas.includes(all_permission) || permissionDatas.includes(
        permission)
    })

    if (!hasPermission) {
      return false
    }
    return true
  } else {
    console.error(`错误的权限标识`)
    return false
  }
}

/**
 * 拼接资源全地址
 * @param {Object} url
 */
export function fullPath(url) {
  if (process.env.NODE_ENV == "development") {
    return `${devBaseUrl}${url}`
  } else {
    return `${prodBaseUrl}${url}`
  }
}

/**
 * 判断url是否是http或https
 * @param {string} path
 * @returns {Boolean}
 */
export function isHttpOrHttps(url) {
  return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1
}

/**
 * 没有token跳转登录
 */
export function authToLogin() {
  if (!getToken()) {
    uni.showToast({
      title: '请先登录',
      icon: 'none'
    })
    uni.reLaunch({
      url: "/pages/login/login"
    })
  }
}

/**
 * 有token跳转首页
 */
export function authToHome() {
  if (getToken()) {
    uni.reLaunch({
      url: "/pages/home/home"
    })
  }
}

/**
 * 清空登录状态
 */
export function removeLoginState() {
  store.commit('user/REMOVE_LOGIN_STATE')
}

/**
 * 字符串分割数组
 * 支持指定分隔符
 * 支持指定数组中值的类型
 * @param {String} str
 * @param {String} dot
 * @param {"String" | "Number" | "Boolean"} type
 * @returns {String[] | Number[] | Boolean[]}
 */
export function string2Array(str, dot = ',', type = 'String') {
  let arr
  if (str.trim()) {
    arr = str.split(dot)
  } else {
    arr = []
  }
  return arr.map((value) => {
    switch (type) {
      case 'String':
        return String(value)
      case 'Number':
        return Number(value)
      case 'Boolean':
        return Boolean(value)
      default:
        return value
    }
  })
}

/**
 * 两数运算 指定保留小数
 * 默认 相乘 保留3位小数
 * @param {Number | String} num1
 * @param {Number | String} num2
 * @param {Object} options
 * @param {Number | String} options.decimal
 * @param {"plus" | "minus" | "times" | "div"} options.type
 * @returns {Number}
 */
export function calc(num1, num2, options = {
  decimal: 3,
  type: 'times'
}) {
  let _num1 = Number(num1)
  let _num2 = Number(num2)
  let _decimal = Number(options.decimal)
  const _type = options.type

  if (isNaN(_num1)) _num1 = 0
  if (isNaN(_num2)) _num2 = 0
  if (isNaN(_decimal)) _decimal = 0

  const num1BigNumber = new BigNumber(_num1)

  if (_type === 'plus') {
    return Number(num1BigNumber.plus(_num2).toFixed(_decimal))
  } else if (_type === 'minus') {
    return Number(num1BigNumber.minus(_num2).toFixed(_decimal))
  } else if (_type === 'times') {
    return Number(num1BigNumber.times(_num2).toFixed(_decimal))
  } else if (_type === 'div') {
    return Number(num1BigNumber.div(_num2).toFixed(_decimal))
  } else {
    return 0
  }
}